package com.github.edu.web.user.controller;

import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.admin.edu.oauth.server.api.entity.TSysOrganization;
import com.github.admin.edu.oauth.server.api.entity.TSysUser;
import com.github.edu.client.common.annotation.*;
import com.github.edu.client.common.controller.BaseController;
import com.github.edu.web.user.service.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-1
 */
@Controller
@RequestMapping("/uc/user")
public class UserManagerController extends BaseController<TSysUser,String> {

    @Autowired
    private UserManagerService service;

    @ModelAttribute
    public void setOrgList(ModelMap modelMap){
        List<TSysOrganization> list=service.getAllOrganization();
        modelMap.put("OrgList",list);
    }

    @QueryAll
    public String index(ModelMap modelMap, TSysUser sysOauth, Pager pager) {
        if(null==pager){
            pager=new Pager();
        }
        pager.setDirection("DESC");
        String[] strs={"name"};
        pager.setOrder(strs);
        JsonArray<TSysUser> jsonArray=service.getAllByPage(sysOauth,pager);
        return super.index(modelMap,sysOauth,jsonArray,"user","用户管理",
                "编码:id","用户姓名:name","登录账号:userid","所属部门:orgname","用户状态:stateName");
    }

    @New
    public String add(ModelMap modelMap, TSysUser sysOauth, HttpServletRequest request) {
        return super.newIndex(modelMap,sysOauth,"user","新增授权服务");
    }

    @Save
    public String saveOrUpdate(ModelMap modelMap, TSysUser sysOauth, HttpServletRequest request) {
        sysOauth=service.saveOrUpdate(sysOauth);
        return super.saveObject(modelMap,sysOauth,"保存成功！","user","保存用户");
    }

    @Open
    public String openEntity(ModelMap modelMap,@PathVariable("id") String id) {
        TSysUser application=service.getEntity(id);
        return super.openEntity(modelMap,application,"user","查看详情");
    }
    @Edit
    public String edit(ModelMap modelMap,@PathVariable("id") String s) {
        TSysUser application=service.getEntity(s);
        return super.newIndex(modelMap,application,"user","修改用户");
    }

    @Delete
    public String delete( String ids) {
        return service.delete(ids);
    }


}
